package com.csdj.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csdj.server.mapper.DepartmentMapper;
import com.csdj.server.pojo.Department;
import com.csdj.server.pojo.RespBean;
import com.csdj.server.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xyx
 * @since 2022-12-12
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired(required = false)
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAllDepartmentById() {
        // -1 == 股东会
        return departmentMapper.getAllDepartmentById(-1);
    }

    @Override
    public RespBean addDep(Department dep) {
        dep.setEnabled(true);   //默认开启部门
        departmentMapper.addDep(dep);
        if(dep.getResult()==1){
            return RespBean.success("添加成功!",dep);
        }
        return RespBean.error("添加失败!");
    }

    @Override
    public RespBean deleteDep(Integer id) {
        Department department = new Department();
        department.setId(id);
        //删除 部门信息 会返回一个对应的结果
        //返回 -2 代表当前部门底下有子部门 不能删除
        //返回 -1 说明当前部门下 有员工信息 不能删除
        //返回 1 删除成功
        departmentMapper.deleteDep(department);
        if(department.getResult()==-2){
            return RespBean.error("该部门下有子部门,删除失败!");
        }
        if(department.getResult()==-1){
            return RespBean.error("说明当前部门下有员工信息,删除失败");
        }
        if(department.getResult()==1){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败!");
    }
}
